// pages/my_ask/my_ask.js

import config from './../../config';
import {
    empty,
    message,
    session,
    getOpenId
} from './../../utils/helper';
import {
    getOrderList,
    wxPayment,
    queryOrder,
    delOrder,
    Evaluation,
} from './../../services/my_ask';
import qs from 'qs';

//获取应用实例
const app = getApp();
//实例化本地缓存
const cookie = new session();

// 需要设置slider的宽度，用于计算中间位置
var sliderWidth = 96;

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tabs: ["最新", "待付款", "待评价"],
        activeIndex: 0,
        sliderOffset: 0,
        disabled: false,
        array_1: [],
        array_2: [],
        array_3: []
    },

    //父级页面传导的参数
    params: {},

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(option) {
        if (empty(option)) {
            this.params = {
                activeIndex: 0
            };
        } else {
            this.params = option;
        }
        this.getDingdanList(this.params.activeIndex);
        this.setData({
            activeIndex: this.params.activeIndex
        });
        //显示当前页面的转发按钮
        wx.showShareMenu({
            withShareTicket: true //是否使用带 shareTicket 的转发详情
        });
        //显示slider
        var that = this;
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
                });
            }
        });
    },

    //追问
    goOrderInfo(e) {
        let that = this,
            params = e.currentTarget.dataset;
        that.setData({
            disabled: true
        });
        wx.navigateTo({
            url: '/pages/free_zx/free_zx?' + qs.stringify({
                order_id: params.order_id
            }),
            success: () => {
                that.setData({
                    disabled: false
                });
            }
        })
    },

    //评价
    reminders(e) {
        let that = this,
            params = e.currentTarget.dataset;
        that.setData({
            disabled: true
        });
        getOpenId(openid => {
            params.openid = openid;
            wx.navigateTo({
                url: '/pages/pingjia/pingjia?' + qs.stringify(params),
                success: () => {
                    that.setData({
                        disabled: false
                    });
                }
            })
        });
    },

    //催单
    evaluation(e) {
        let that = this,
            params = e.currentTarget.dataset,
            curTime = new Date().getTime(),
            startTime = new Date(Date.parse(params.create_time)).getTime();
        if (curTime - startTime <= 900 * 1000) {
            const y = Math.round((900 * 1000 - (curTime - startTime)) / (60 * 1000));
            message('请' + y + "分钟以后再催单")
        } else {
            that.setData({
                disabled: true
            });
            getOpenId(openid => {
                params.openid = openid
                Evaluation(params, {
                    success: () => {
                        wx.showToast({
                            title: '催单成功',
                            icon: 'success',
                            duration: 2000
                        })
                    },
                    complete: () => {
                        that.setData({
                            disabled: false
                        });
                    }
                });
            });
        }
    },

    //删除订单
    del(e) {
        let that = this,
            params = e.currentTarget.dataset;
        wx.showModal({
            content: '确定要删除吗？',
            showCancel: false,
            success: function(res) {
                if (res.confirm) {
                    that.setData({
                        disabled: true
                    });
                    delOrder(params, {
                        success: res => {
                            that.getDingdanList(that.params.activeIndex);
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                duration: 2000
                            })
                        },
                        complete: () => {
                            that.setData({
                                disabled: false
                            });
                        }
                    });
                }
            }
        })
    },

    //付款
    payment: function(e) {
        let that = this,
            params = e.currentTarget.dataset;
        that.setData({
            disabled: true
        });
        queryOrder(params, {
            success: (res) => {
                wxPayment(res.data, {
                    success: function(res) {
                        wx.showModal({
                            content: '支付成功',
                            showCancel: false,
                            success: function(res) {
                                if (res.confirm) {
                                    wx.startPullDownRefresh()
                                }
                            }
                        })
                    },
                    fail: function(res) {
                        message('支付失败,请重试')
                    },
                    complete: function(res) {
                        that.setData({
                            disabled: false
                        });
                    }
                })
            },
            fail: (res) => {
                that.setData({
                    disabled: false
                });
                message(res.msg)
            }
        });
    },

    //获取微信用户的订单列表
    getDingdanList: function(status) {
        let params = {
                status: status,
            },
            that = this;
        getOpenId(openid => {
            params.openid = openid;
            getOrderList(params, {
                success: res => {
                    if (res.option == '0') {
                        that.setData({
                            array_1: res.data,
                        });
                    } else if (res.option == '1') {
                        that.setData({
                            array_2: res.data,
                        });
                    } else if (res.option == '2') {
                        that.setData({
                            array_3: res.data,
                        });
                    }
                }
            });
        })
    },

    // 导航按钮变化行为触发状态机变化
    tabClick: function(e) {
        this.params.activeIndex = e.currentTarget.id;
        this.getDingdanList(this.params.activeIndex);
        this.setData({
            sliderOffset: e.currentTarget.offsetLeft,
            activeIndex: this.params.activeIndex
        });
    },

    //下拉刷新事件
    onPullDownRefresh() {
        this.onLoad(this.params);
        this.getDingdanList(this.params.activeIndex);
        wx.stopPullDownRefresh();
    },

})